package com.qianxx.viewlibrary.recyclerview.pull.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
    BaseAdapter adapter;
    int disallowMoveIndex = -1;

    public SimpleItemTouchHelperCallback(BaseAdapter adapter) {
        this.adapter = adapter;
    }

    /**
     * 设置滑动方向
     *
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder.getAdapterPosition() != disallowMoveIndex) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//允许上下左右的拖动
            int swipeFlags = ItemTouchHelper.LEFT;   //只允许从右向左侧滑
            return makeMovementFlags(dragFlags, swipeFlags);
        }
        return 0;
    }

    /**
     * 设置不允许滑动的item
     *
     * @param disallowMoveIndex
     */
    public void setDisallowMoveItem(int disallowMoveIndex) {
        this.disallowMoveIndex = disallowMoveIndex;
    }

    /**
     * 交换位置
     *
     * @param recyclerView
     * @param viewHolder
     * @param target
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return false;
    }

    /**
     * 滑动删除
     *
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        adapter.onItemDissmiss(viewHolder.getAdapterPosition());
    }

    /**
     * 长按才能滑动
     *
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    /**
     * 允许滑动删除（即允许执行onSwiped回调方法）
     *
     * @return
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }
}
